package com.fts.fts.fitness_tracking_system.service.impl;

import com.fts.fts.fitness_tracking_system.service.ISysBaseAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SysBaseApiImpl implements ISysBaseAPI {
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void sendVerificationCode(String toEmail, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("15157729007@163.com");
        message.setTo(toEmail);
        message.setSubject("iKeeping验证码");
        message.setText("您的验证码是：" + code + "，有效期为5分钟");
        mailSender.send(message);
    }

    public void saveCode(String email, String code) {
        // 存储验证码，5分钟过期
        redisTemplate.opsForValue().set(email, code, 5, TimeUnit.MINUTES);
    }

    public String getCode(String email) {
        return redisTemplate.opsForValue().get(email);
    }

    // 删除验证码
    public void deleteCode(String email) {
        redisTemplate.delete(email); // 直接删除键
    }
}
